package com.xxx.video.mine.activitys;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;

import com.xxx.video.R;
import com.xxx.video.commen.activitys.BaseActivity;
import com.xxx.video.commen.configs.Constant;
import com.xxx.video.commen.utils.FontCacheUtils;
import com.xxx.video.commen.utils.PlayVoiceUtils;
import com.xxx.video.commen.utils.RecordVoiceUtils;
import com.xxx.video.commen.utils.StatusBarUtil;
import com.xxx.video.commen.utils.StringUtils;
import com.xxx.video.commen.utils.ToastUtils;
import com.xxx.video.commen.views.FontTextView;
import com.xxx.video.mine.presenters.AddAudioPresenter;
import com.xxx.video.mine.presenters.IAddAudioView;

import butterknife.BindView;
import butterknife.OnClick;

public class AddAudioActivity extends BaseActivity<AddAudioPresenter> implements IAddAudioView, PlayVoiceUtils.OnCompletionListener, RecordVoiceUtils.OnRecordListener {
    private static final int REQUEST_RECORD_AUDIO_PERMISSION = 200;
    @BindView(R.id.tv_add_audio_activity_time)
    TextView tvAddAudioActivityTime;
    @BindView(R.id.et_add_audio_activity_video_title)
    EditText etAddAudioActivityVideoTitle;
    @BindView(R.id.tv_add_audio_activity_record)
    TextView tvAddAudioActivityRecord;
    @BindView(R.id.tv_add_audio_activity_play)
    TextView tvAddAudioActivityPlay;
    private String filePath;
    private String[] permissions = {Manifest.permission.RECORD_AUDIO, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
    private boolean isRecord = false;
    private boolean isPlay = false;

    @Override
    public void initToolbarData() {
        super.initToolbarData();
        showActionBar();
        StatusBarUtil.getInstance().setStatusBarTransparent(this, true);
        setIsShowActionbarLine(true);
        setActionBarTitle(R.string.title_add_audio_activity);
        Typeface customFont = FontCacheUtils.getInstance().getTypeface(FontTextView.ttf3, this);
        etAddAudioActivityVideoTitle.setTypeface(customFont);
    }

    @Override
    protected AddAudioPresenter createPresenter() {
        return new AddAudioPresenter(this, this);
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_add_audio;
    }

    @Override
    public void initData(Bundle extras) {
        showRootLayoutStatus(1);
    }

    @Override
    public void showRootLayoutStatus(int status) {
        showLayoutStatus(status);
    }


    @OnClick({R.id.tv_add_audio_activity_play, R.id.tv_add_audio_activity_record, R.id.tv_add_audio_activity_record_cancel, R.id.tv_add_audio_activity_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_add_audio_activity_play:
                if (isPlay) {
                    isPlay = false;
                    RecordVoiceUtils.getInstance().stopRecord();
                    tvAddAudioActivityPlay.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.icon_add_audio_record_play_uncheck, 0, 0);
                } else {
                    isPlay = true;
                    PlayVoiceUtils.getInstance().startPlayVoice(filePath, 0);
                    PlayVoiceUtils.getInstance().setOnCompletionListener(this);
                    tvAddAudioActivityPlay.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.icon_add_audio_record_play, 0, 0);
                }
                tvAddAudioActivityRecord.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.icon_add_audio_record_uncheck, 0, 0);
                break;
            case R.id.tv_add_audio_activity_record:
                PlayVoiceUtils.getInstance().stopVoice(true);
                if (isRecord) {
                    isPlay = false;
                    RecordVoiceUtils.getInstance().stopRecord();
                    isRecord = false;
                    tvAddAudioActivityRecord.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.icon_add_audio_record_uncheck, 0, 0);
                } else {
                    tvAddAudioActivityTime.setText("00:00");
                    filePath = "";
                    requestRecordAudioPermission();
                    isRecord = true;
                    tvAddAudioActivityRecord.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.icon_add_audio_record, 0, 0);
                }
                break;
            case R.id.tv_add_audio_activity_record_cancel:
                tvAddAudioActivityTime.setText("00:00");
                filePath = "";
                isPlay = false;
                RecordVoiceUtils.getInstance().stopRecord();
                tvAddAudioActivityRecord.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.icon_add_audio_record_uncheck, 0, 0);
                break;
            case R.id.tv_add_audio_activity_submit:
                getPresenter().submit(RecordVoiceUtils.getInstance().getTime());
                break;
            default:
                break;
        }
    }

    @Override
    public void requestRecordAudioPermission() {
        ActivityCompat.requestPermissions(this, permissions, REQUEST_RECORD_AUDIO_PERMISSION);
    }

    @Override
    public String getRecordVoicePath() {
        return filePath;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case REQUEST_RECORD_AUDIO_PERMISSION:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED && grantResults[2] == PackageManager.PERMISSION_GRANTED) {
                    filePath = Constant.RECORD_AUDIO_PATH + System.currentTimeMillis() + ".mp3";
                    RecordVoiceUtils.getInstance().startRecord(filePath);
                    RecordVoiceUtils.getInstance().setOnRecordListener(this);
                } else {
                    /*
                     * no permission
                     */
                    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_please_open_the_read_and_write_external_storage_permission));
                    } else {
                        ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_please_open_the_record_audio_permission));
                    }
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onProgress(int progress) {

    }

    @Override
    public void onPlayStop() {

    }

    @Override
    public void onRecordProgress(int progress) {
        String time = "";
        int hour = progress / 60000;
        int seconds = progress % 60000 / 1000;
        if (hour < 10) {
            time = "0" + hour;
        } else {
            time = String.valueOf(hour);
        }
        if (seconds < 10) {
            time = time + ":0" + seconds;
        } else {
            time = time + ":" + seconds;
        }
        tvAddAudioActivityTime.setText(time);
    }

    @Override
    public void releaseSuccess() {
        ToastUtils.showSuccess(StringUtils.getInstance().getString(R.string.tip_add_voice_success));
        setResult(RESULT_OK);
        finish();
    }

    @Override
    public String getAudioTitle() {
        return etAddAudioActivityVideoTitle.getText().toString().trim();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        PlayVoiceUtils.getInstance().stopVoice(true);
        RecordVoiceUtils.getInstance().stopRecord();
    }
}
